/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2021.
 * Description: kernelspace API for using kbox regions
 * Author: huangshaobo
 * Create: 2020-11-18
 */
#ifndef SET_KBOX_REGION_H
#define SET_KBOX_REGION_H
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/kbox/kbox.h>
#define REGIONID_BITS 16
#define CONTEXT_BITS 26
#define REGIONID_MASK ((1 << REGIONID_BITS) - 1)
#define CONTEXT_LEVEL_MASK (~REGIONID_MASK)
#define CONTEXT_MASK (~((1 << CONTEXT_BITS) - 1))

#define PROCESS_CONTEXT (1 << CONTEXT_BITS)
#define SOFTIRQ_CONTEXT (2 << CONTEXT_BITS)
#define HARDIRQ_CONTEXT (3 << CONTEXT_BITS)

#define KBOX_CONF_PANIC (PROCESS_CONTEXT | KBOX_DEFAULT_REG_ID)


#if (defined CONFIG_RTOS_KBOX)
extern int kern_kbox_write(int fd, const char *buf, unsigned len);
#else
static inline int kern_kbox_write(int fd, const char *buf, unsigned len)
{
	return -EINVAL;
}
#endif

#if (defined CONFIG_RTOS_KBOX) && !(defined CONFIG_RTOS_KBOX_MINI)
extern void init_kbox_region(struct task_struct *p);
extern int __attribute__((warn_unused_result)) set_kbox_region(int mod_region);
extern void restore_kbox_region(int old_setting);
extern int get_kbox_region(void);
extern int kern_kbox_register_region(const char *name, size_t size, struct module *mod);
#else
static inline void init_kbox_region(struct task_struct *p)
{
	return;
}

static inline int __attribute__((warn_unused_result)) set_kbox_region(int mod_region)
{
	return 0;
}

static inline void restore_kbox_region(int old_setting)
{
	return;
}

static inline int get_kbox_region(void)
{
	return KBOX_DEFAULT_REG_ID;
}

static inline int kern_kbox_register_region(const char *name, size_t size, struct module *mod)
{
	return -EINVAL;
}
#endif

#endif
